package com.cloudcc.mobile.dao.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.cloudcc.cloudframe.bus.EventList;
import com.cloudcc.cloudframe.bus.EventList.PushRegisterEvent;
import com.cloudcc.cloudframe.model.param.PushRegisterParam;
import com.cloudcc.cloudframe.net.async.AsyncClient;
import com.cloudcc.cloudframe.net.async.GsonHttpResponseHandler;
import com.cloudcc.cloudframe.net.model.BaseNetModel;
import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.dao.PushEngine;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.NetWork;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.util.UrlTools;
import com.litesuits.android.log.Log;
import com.loopj.android.http.RequestParams;

public class PushEngineImpl implements PushEngine {

	@Override
	public String sendDNS(String data) {
		String url = "";
		String jsonString = "";
		try {
			String Data = URLEncoder.encode(data, "utf-8");// 这个是对json数据转编码，必须的
			url = UrlTools.url + UrlTools.url_path
					+ "serviceName=sendDNS&userId="
					+ UserManager.getManager().getUser().userId
					+ "&svcer=umengPush&data=[" + Data + "]&binding="
					+ AppContext.binding;
			jsonString = NetWork.get(url);
			Log.d("senddns", "sfgvdf" + jsonString);
		} catch (UnsupportedEncodingException e) {
			Log.d("senddns", "sfgvdf" + e);
			Tools.handle(e);
		}
		return jsonString;
	}

	@Override
	public void registerPush(PushRegisterParam param, PushRegisterEvent event) {
		String data = GsonUtil.Object2Json(param);
		RequestParams params = new RequestParams();
		params.add("serviceName", "sendDNS");
		params.add("userId", UserManager.getManager().getUser().userId);
		params.add("svcer", "umengPush");
		params.add("data", "[" + data + "]");
		params.add("binding", RunTimeManager.getInstance().getServerBinding());

		AsyncClient.getInstance().post(
				UrlManager.getInterfaceUrl(),
				params,
				new GsonHttpResponseHandler<BaseNetModel>(BaseNetModel.class,
						event));
	}

	@Override
	public void cancelDSN(EventList.PushCancleEvent event) {
		RequestParams params = new RequestParams();
		params.add("serviceName", "cancelDNS");
		params.add("userId", UserManager.getManager().getUser().userId);
		params.add("binding", RunTimeManager.getInstance().getServerBinding());
		AsyncClient.getInstance().post(
				UrlManager.getInterfaceUrl(),
				params,
				new GsonHttpResponseHandler<BaseNetModel>(BaseNetModel.class,
						event));
	}

}
